package common;

import objects.GameServer;

public class PacketFormatter
{
	public static final String getPolicyFilePacket()
	{
		String packet = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +   
		"<cross-domain-policy>"+  
	    "<allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" />"+  
	    "<site-control permitted-cross-domain-policies=\"master-only\" />"+  
	    "</cross-domain-policy>";
		return packet;
	}

	public static String getHelloConnectionPacket(String hashKey)
	{
		return "HC"+hashKey;
	}

	public static final String getWrongVersionPacket()
	{
		return "AlEv1.29.1";
	}

	public static final String getAccountBannedPacket()
	{
		return "AlEb";
	}

	public static final String getAccountConnexionDefaultErrorPacket()
	{
		return "AlEE";
	}

	public static final String getInvalidAccountPacket()
	{
		return "AlEf";
	}

	public static String getAccountDofusPseudoPacket(String pseudo)
	{
		return "Ad"+pseudo;
	}

	public static String getAccountCommunityPacket(int community)
	{
		return "Ac"+community;
	}

	public static String getAccountLoginOkPacket(boolean gm)
	{
		return "AlK"+(gm?"1":"0");
	}

	public static String getAccountQuestionPacket(String question) 
	{
		return "AQ"+question.replace(" ", "+");
	}

	public static String getAccountHostListPacket(boolean isSubscriber)
	{
		String packet = "AH";
		//AH[ID];[State];[Completion];[CanLog]
		synchronized(RealmServer.getGameServers())
		{
			for(GameServer GS : RealmServer.getGameServers().values())
			{
				//If there is more than 1 GameServer
				if(packet.length() != 2)packet += "|";
				//Can Log only if subscriber
				packet += GS.getId()+";"+GS.getState()+";"+GS.getCompletion()+";"+(isSubscriber?"1":"0");
			}
		}
		return packet;
	}
	
	public static String getAccountWaitingQueuePacket(short position, short totalAbo, short totalNonAbo, boolean subscribe,byte queueID)
	{
		return "Af"+position+"|"+totalAbo+"|"+totalNonAbo+"|"+(subscribe?"1":"0")+"|"+queueID;
	}

	public static String getAccountServerListPacket(long guid, long subscribeTimeLeft)
	{
		String packet = "AxK"+subscribeTimeLeft;
		synchronized(RealmServer.getGameServers())
		{
			for(GameServer GS : RealmServer.getGameServers().values())
			{
				byte n = SQLMgr.getCharNumbByAccount(GS.getId(),guid);
				if(n ==0)continue;
				packet += "|"+GS.getId()+","+n;
			}
		}
		return packet;
	}

	public static final String getISAcceptedPasswordPacket()
	{
		return "00";
	}

	public static final String getISAcceptedInformationsPacket()
	{
		return "01";
	}
	
	public static final String getISUpdateStateOkPacket()
	{
		return "10";
	}

	public static String getCryptedGameServerAddressPacket(String ip, short port, long guid)
	{
		return "AXK"+CryptMgr.CryptIP(ip)+CryptMgr.CryptPort(port)+guid;
	}

	public static String getClearGameServerAddressPacket(String ip, short port, long guid)
	{
		return "AYK"+ip+":"+port+";"+guid;
	}

	public static String getISRegisterAccountInWQPacket(long guid,byte gmLevel, String answer,
									String pseudo, long subscribeEnd, int gsIDSelected)
	{
		return "02"+guid+";"+pseudo+";"+gmLevel+";"+answer+";"+subscribeEnd+";"+gsIDSelected;
	}
        
}
